<?php
// +----------------------------------------------------------------------
// | Author: weifashi <605403358@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\actions\controls;
use app\admin\actions\Methods;

error_reporting(0);

class SearchService {
    
	//基础定义
	private $searchNode = '';
		
	/**
    * 构造函数自动执行
    * @return null
    */
    public  function __construct($xml=null,$id=null){
        //错误级别
		error_reporting(E_ERROR | E_PARSE);
        // 
    	if($xml && !$id){
    		$this->searchNode = $xml;
     	}else{
	    	$param = request()->param();
	    	$nodeId = $id ?: $param['id'] ?: '';
	    	$xmlFile = Methods::getXmlFile($xml);
	    	if(!$this->searchNode = self::isExistNode($xmlFile,$nodeId)){
	    		throw new \think\Exception('Search-找不到xml对应节点，或xml文件格式错误！');
	    	}
        }
        
    }
	    
    /**
     * 获取示例
     * @param array $options 实例配置
     * @return static
     */
    public static function instance($p1=null,$p2=null,$p3=null)
    {
        return new self($p1,$p2,$p3);
    }
    
    /**
    * @access public
    * @param string $xml        
    * @param string $id             
    * @return array
    */
    public function getOption(){
    	$search = $this->searchNode;
    	$data = array();
        foreach($search->attributes() as  $key => $attr ){
            $data["$key"]="$attr";
        }
        
        $qian=array("\t","\n","\r");
        $data["htmls"] = htmlspecialchars(str_replace($qian, '', $search->htmls));
        
        $data["query"] = array();
        foreach($search->row as $row ){
            $query=array();
            foreach($row->attributes() as  $key => $attr ){
                $query["$key"]= self::replaceExp("$attr",$row);
                $query["options"]= array();
                if($row->option){
                    foreach($row->option as $option){
                        $arr=array();
                        foreach($option->attributes() as  $k => $a ){
                            $arr["$k"]= self::replaceExp("$a");
                        }
                        if(!$arr["label"]){
                            $arr["label"] = self::replaceExp("$option");
                        }
                        array_push($query["options"], $arr);
                    }
                }
                if(($key=="url" || $key=="dataUrl") && empty($attr.'')!=true){
                    $query["url"]="";
                    $query["dataUrl"]="";
                    $selectdata = Methods::callApi($attr);
                    if( !is_array($selectdata) ){ $selectdata = $selectdata->getData()['data']; }
                    foreach($selectdata as  $h ){
                        array_push($query["options"],$h);
                    }
                }
            }
            array_push($data["query"], $query);
        }
        
        $data["btns"] = array();
        foreach($search->button as $button ){
            $btns=array();
            foreach($button->attributes() as  $key => $attr ){
                $btns["$key"]= self::replaceExp("$attr");
            }
            array_push($data["btns"], $btns);
        }
        
        if($search->queryMores){
            $data["queryMore"] = array();
            foreach($search->queryMores->row as $row ){
                $queryMore=array();
                foreach($row->attributes() as  $key => $attr ){
                    $queryMore["$key"]= self::replaceExp("$attr");
                    if($row->option){
                        $queryMore["options"]= array();
                        foreach($row->option as $option){
                            $arr=array();
                            foreach($option->attributes() as  $k => $a ){
                                $arr["$k"]= self::replaceExp("$a");
                            }
                            if(!$arr["label"]){
                                $arr["label"] = self::replaceExp("$option");
                            }
                            array_push($queryMore["options"], $arr);
                        }
                    }
                }
                array_push($data["queryMore"], $queryMore);
            }
        }
        
        return $data;
    } 
    
    /**
    * 替换占位符
    * /admin/Api/Grid/getOption
    * @access private
    * @param array $data        
    * @return array
    */
    private function replaceExp($str,$row=null){
    	return Methods::replaceExp($str,$row);
    }
    
    
    /**
    * 是否存在grid节点
    * /admin/Api/Grid/getOption
    * @param string $xmlFile   			xml-id
    * @return  bu
    */
	private function isExistNode( $xmlFile,$id ){
		$node = false;
		foreach( $xmlFile->search as $val){
	    	if( $id == $val->attributes()["id"] ){
	    		$node = $val;
	    	}
		}
		return $node;
	}
    
}




